<?php
namespace app\source\controller\card;

use app\common\controller\Api;

/**
 * 通讯录分类接口
 */
class Type extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];

    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
        
    public function __construct()
    {
        parent::__construct();

        $this->BusinessModel = model('Business');
        $this->TypeModel = model('Card.Type');
        $this->RegionModel = model('Region');
    }

    /**
     * 查询通讯录分类
     *
     * @ApiTitle    (查询通讯录分类)
     * @ApiMethod   (POST)
     * @ApiRoute    (/source/card/type/index)
     * @ApiParams   (name="page", type="string", required=true, description="当前页数")
     * @ApiParams   (name="limit", type="string", required=true, description="每页显示多少条数据")
     * @ApiParams   (name="businessid", type="string", required=true, description="用户ID")
     * @ApiParams   (name="search", type="string", required=true, description="搜索关键词")
     * @ApiReturnParams   (name="code", type="integer", sample="0", description="状态码")
     * @ApiReturnParams   (name="msg", type="string", sample="返回成功", description="返回信息")
     * @ApiReturnParams   (name="data", type="object", sample="null", description="返回数据")
     * @ApiReturn   ({
            "code": 1,
            "msg": "查询通讯录分类",
            "time": "时间戳",
            "data": {
                "list": [  //分类列表数据
                    {
                        "id": 26,
                        "name": "影院"
                    },
                    ....更多数据
                ],
                "count": 25 //总条数
            }
        })
     */
    public function index()
    {
        if($this->request->isPost())
        {
            $page = $this->request->param('page', 1, 'trim');
            $limit = $this->request->param('limit', 10, 'trim');
            $businessid = $this->request->param('businessid', 0, 'trim');
            $search = $this->request->param('search', '', 'trim');

            //根据ID条件查找用户
            $business = $this->BusinessModel->where(['id' => $businessid])->find();
            
            if(!$business)
            {
                $this->error('用户不存在');
                exit;
            }

            $where = [];

            if(!empty($search))
            {
                $where['name'] = ['like', "%$search%"];
            }

            //总条数
            $count = $this->TypeModel->where($where)->count();

            // 偏移量
            $start = ($page-1)*$limit;

            $list = $this->TypeModel->where($where)->order('id desc')->limit($start, $limit)->select();

            if($list)
            {
                $data = [
                    'list'=>$list,
                    'count'=>$count
                ];
                $this->success('返回数据成功', $data);
                exit;
            }else
            {
                $this->error('暂无更多数据');
                exit;
            }
        }
    }

    /**
     * 添加通讯录分类
     *
     * @ApiTitle    (添加通讯录分类)
     * @ApiMethod   (POST)
     * @ApiRoute    (/source/card/type/add)
     * @ApiParams   (name="businessid", type="string", required=true, description="会员ID")
     * @ApiParams   (name="name", type="string", required=true, description="分类名称")
     * @ApiReturnParams   (name="code", type="integer", sample="0", description="状态码")
     * @ApiReturnParams   (name="msg", type="string", sample="返回成功", description="返回信息")
     * @ApiReturnParams   (name="data", type="object", sample="null", description="返回数据")
     * @ApiReturn   ({
            "code": 1,
            "msg": "添加通讯录分类成功",
            "time": "时间戳",
            "data": {}
        })
     */
    public function add()
    {
        if($this->request->isPost())
        {
            $businessid = $this->request->param('businessid', 0, 'trim');

            $business = $this->BusinessModel->where(['id' => $businessid])->find();
            
            if(!$business)
            {
                $this->error('会员信息不存在');
                exit;
            }

            //组装数据
            $data = [
                'name' => $this->request->param('name', '', 'trim'),
            ];
            
            $result = $this->TypeModel->validate('common/Card/Type')->save($data);

            if($result === FALSE)
            {
                $this->error($this->TypeModel->getError());
                exit;
            }else
            {
                $this->success('添加通讯录分类成功');
                exit;
            }
        }
    }

    /**
     * 删除通讯录分类
     *
     * @ApiTitle    (删除通讯录分类)
     * @ApiMethod   (POST)
     * @ApiRoute    (/source/card/type/del)
     * @ApiParams   (name="businessid", type="string", required=true, description="会员ID")
     * @ApiParams   (name="typeid", type="string", required=true, description="删除的分类ID")
     * @ApiReturnParams   (name="code", type="integer", sample="0", description="状态码")
     * @ApiReturnParams   (name="msg", type="string", sample="返回成功", description="返回信息")
     * @ApiReturnParams   (name="data", type="object", sample="null", description="返回数据")
     * @ApiReturn   ({
            "code": 1,
            "msg": "删除通讯录分类成功",
            "time": "时间戳",
            "data": null
        })
     */
    public function del()
    {
        if($this->request->isPost())
        {
            $businessid = $this->request->param('businessid', 0, 'trim');
            $typeid = $this->request->param('typeid', 0, 'trim');

            //根据ID条件查找用户
            $business = $this->BusinessModel->where(['id' => $businessid])->find();
            
            if(!$business)
            {
                $this->error('用户不存在');
                exit;
            }

            //判断删除的通讯录是否存在
            $card = $this->TypeModel->where(['id' => $typeid])->find();

            if(!$card)
            {
                $this->error('要删除的分类不存在');
                exit;
            }
            
            $result = $this->TypeModel->destroy($typeid);

            if($result === FALSE)
            {
                $this->error($this->TypeModel->getError());
                exit;
            }else
            {
                $this->success('删除通讯录分类成功');
                exit;
            }
        }
    }

    /**
     * 编辑通讯录分类
     *
     * @ApiTitle    (编辑通讯录分类)
     * @ApiMethod   (POST)
     * @ApiRoute    (/source/card/type/edit)
     * @ApiParams   (name="typeid", type="string", required=true, description="通讯录分类ID")
     * @ApiParams   (name="businessid", type="string", required=true, description="会员ID")
     * @ApiParams   (name="name", type="string", required=true, description="昵称")
     * @ApiReturnParams   (name="code", type="integer", sample="0", description="状态码")
     * @ApiReturnParams   (name="msg", type="string", sample="返回成功", description="返回信息")
     * @ApiReturnParams   (name="data", type="object", sample="null", description="返回数据")
     * @ApiReturn   ({
            "code": 1,
            "msg": "编辑通讯录分类成功",
            "time": "时间戳",
            "data": {}
        })
     */
    public function edit()
    {
        if($this->request->isPost())
        {
            $typeid = $this->request->param('typeid', 0, 'trim');
            $businessid = $this->request->param('businessid', 0, 'trim');

            //判断通讯录分类是否存在
            $type = $this->TypeModel->where(['id' => $typeid])->find();
            
            if(!$type)
            {
                $this->error('通讯录分类信息不存在');
                exit;
            }

            $business = $this->BusinessModel->where(['id' => $businessid])->find();
            
            if(!$business)
            {
                $this->error('会员信息不存在');
                exit;
            }

            //组装数据
            $data = [
                'id' => $typeid,
                'name' => $this->request->param('name', '', 'trim'),
            ];
            
            $result = $this->TypeModel->validate('common/Card/Type')->isUpdate(true)->save($data);

            if($result === FALSE)
            {
                $this->error($this->TypeModel->getError());
                exit;
            }else
            {
                $this->success('更新通讯录分类成功');
                exit;
            }
        }
    }

    /**
     * 检查通讯录分类是否存在
     *
     * @ApiTitle    (检查通讯录分类是否存在)
     * @ApiMethod   (POST)
     * @ApiRoute    (/source/card/type/check)
     * @ApiParams   (name="businessid", type="string", required=true, description="会员ID")
     * @ApiParams   (name="typeid", type="string", required=true, description="通讯录分类ID")
     * @ApiReturnParams   (name="code", type="integer", sample="0", description="状态码")
     * @ApiReturnParams   (name="msg", type="string", sample="返回成功", description="返回信息")
     * @ApiReturnParams   (name="data", type="object", sample="null", description="返回数据")
     * @ApiReturn   ({
            "code": 1,
            "msg": "查询通讯录分类成功",
            "time": "时间戳",
            "data": {
                "id": 26,
                "name": "影院"
            }
        })
     */
    public function check()
    {
        if($this->request->isPost())
        {
            $businessid = $this->request->param('businessid', 0, 'trim');
            $typeid = $this->request->param('typeid', 0, 'trim');

            //根据ID条件查找用户
            $business = $this->BusinessModel->where(['id' => $businessid])->find();
            
            if(!$business)
            {
                $this->error('用户不存在');
                exit;
            }

            //判断删除的通讯录分类是否存在
            $type = $this->TypeModel->where(['id' => $typeid])->find();
            
            if($type)
            {
                $this->success('查询通讯录分类成功', $type);
                exit;
            }else
            {
                $this->error('通讯录分类不存在');
                exit;
            }
        }
    }
}